﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.teachers
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        //student stud = new student();
        parent par = new parent(); user_types ut = new user_types(); teacher teach = new teacher();
        Classes cl = new Classes();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lbuser.Visible = false; lbpass.Visible = false;
                tbusername.Visible = false; tbpassword.Visible = false;
                btnupdate.Visible = false;
                DataTable dt1 = ut.GetRecords(-99, "", false, " and ");
                DropDownList1.DataSource = dt1;
                DropDownList1.DataTextField = "user_type";
                DropDownList1.DataValueField = "user_type_id";
                DropDownList1.DataBind();

                DataTable dt = par.GetRecords(-99, "", "", "", "", "", "", "", -99, "", -99, "", "", Convert.ToInt64(Request["id"].ToString()),"","", false, " and ");
                DataTable dt3 = teach.GetRecords(Convert.ToInt64(Session["userid"]), "", "", "", "", "", "", -99, -99, -99, "", "","","","", false, " and ");
                DataTable dt2 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
                if (!(dt2.Rows.Count > 0))
                {
                    if (dt.Rows.Count > 0)
                    {
                        //DetailsView2.DataSource = dt;
                        //DetailsView2.DataBind();
                        //Panel1.Visible = false;
                        DetailsView1.Visible = true; DetailsView2.Visible = false;
                        DetailsView1.DataSource = dt;
                        DetailsView1.DataBind();
                        Panel1.Visible = false;
                    }
                }
                if (dt2.Rows.Count > 0)
                {
                    if (dt.Rows.Count > 0)
                    {
                        DetailsView1.Visible = false; DetailsView2.Visible = true;
                        DetailsView2.DataSource = dt;
                        DetailsView2.DataBind();
                        Panel1.Visible = false;
                        //DetailsView1.DataSource = dt;
                        //DetailsView1.DataBind();
                        //Panel1.Visible = false;
                    }
                }
               
            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            Panel1.Visible = true; btnupdate.Visible = true; btninsert.Visible = false;
            if (e.CommandName == "Edit")
            {
                DataTable dt = par.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", "", "", "", "", "", -99, "", -99, "", "", -99,"","", false, " and ");
                tbparid.Text = dt.Rows[0]["parent_id"].ToString();
                tbfname.Text = dt.Rows[0]["father_name"].ToString();
                tbmname.Text = dt.Rows[0]["mother_name"].ToString();
                tbfquali.Text = dt.Rows[0]["father_qualification"].ToString();
                tbmquali.Text = dt.Rows[0]["mother_qualification"].ToString();
                tbfoccu.Text = dt.Rows[0]["father_occupation"].ToString();
                tbmoccu.Text = dt.Rows[0]["mother_occupation"].ToString();
                tbaddress.Text = dt.Rows[0]["permanent_address"].ToString();
                tbpno.Text = dt.Rows[0]["phone_no"].ToString();
                tbemail.Text = dt.Rows[0]["parent_emailid"].ToString();
                DropDownList1.SelectedValue = dt.Rows[0]["user_type_id"].ToString();
                tbusername.Text=dt.Rows[0]["username"].ToString();
                tbpassword.Text=dt.Rows[0]["password"].ToString();
                tbstudid.Text=dt.Rows[0]["student_id"].ToString();
             }
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt=par.GetRecords(Convert.ToInt64(e.CommandArgument),"","","","","","","",-99,"",-99,"","",-99,"","",false," and ");
                par.parent_id = Convert.ToInt64(dt.Rows[0]["parent_id"].ToString());
                par.father_name = dt.Rows[0]["father_name"].ToString();
                par.mother_name = dt.Rows[0]["mother_name"].ToString();
                par.father_qualification = dt.Rows[0]["father_qualification"].ToString();
                par.mother_qualification = dt.Rows[0]["mother_qualification"].ToString();
                par.father_occupation = dt.Rows[0]["father_occupation"].ToString();
                par.mother_occupation = dt.Rows[0]["mother_occupation"].ToString();
                par.permanent_address = dt.Rows[0]["permanent_address"].ToString();
                par.phone_no=Convert.ToInt64(dt.Rows[0]["phone_no"].ToString());
                par.parent_emailid = dt.Rows[0]["parent_emailid"].ToString();
                par.user_type_id = Convert.ToInt64(dt.Rows[0]["user_type_id"].ToString());
                par.username = dt.Rows[0]["username"].ToString();
                par.password = dt.Rows[0]["password"].ToString();
                par.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                par.isdeleted = true;
                par.SaveRecords();
                Response.Redirect("~/teachers/TSProfile.aspx");
            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            par.parent_id = -99;
            par.father_name = tbfname.Text;
            par.mother_name = tbmname.Text;
            par.father_qualification = tbfquali.Text;
            par.mother_qualification = tbmquali.Text;
            par.father_occupation = tbfoccu.Text;
            par.mother_occupation = tbmoccu.Text;
            par.permanent_address = tbaddress.Text;
            par.phone_no = Convert.ToInt64(tbpno.Text);
            par.parent_emailid = tbemail.Text;
            par.user_type_id = Convert.ToInt64(DropDownList1.SelectedValue.ToString());
            par.student_id = Convert.ToInt64(Request["id"].ToString());
            //par.username = tbusername.Text;
            //par.password = tbpassword.Text;
            par.username = "P" + tbfname.Text.Substring(0, 2) + tbmname.Text.Substring(0, 2);
            par.password = "P" + tbmname.Text.Substring(0, 2) + tbfname.Text.Substring(0, 2);
            par.isdeleted = false;
            par.SaveRecords();
            Response.Redirect("~/teachers/TSProfile.aspx");
        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {

        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            par.parent_id = Convert.ToInt64(tbparid.Text);
            par.father_name = tbfname.Text;
            par.mother_name = tbmname.Text;
            par.father_qualification = tbfquali.Text;
            par.mother_qualification = tbmquali.Text;
            par.father_occupation = tbfoccu.Text;
            par.mother_occupation = tbmoccu.Text;
            par.permanent_address = tbaddress.Text;
            par.phone_no = Convert.ToInt64(tbpno.Text);
            par.parent_emailid = tbemail.Text;
            par.user_type_id=Convert.ToInt64(DropDownList1.SelectedValue.ToString());
            par.username=tbusername.Text;
            par.password=tbpassword.Text;
            par.student_id=Convert.ToInt64(tbstudid.Text);
            par.isdeleted=false;
            par.SaveRecords();
            Response.Redirect("~/teachers/TSProfile.aspx");
            
        }
    }
}
